<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		getEditorTitle,
		formModelLoad,
		hasEditor,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				requirementCache:"mkt-dd-salesOrderReview-requirement-cache",
				oldVal:"",
				userCacheKey:"mkt-dd-salesOrderReview-user-cache",
				resultUserCacheKey:"mkt-dd-salesOrderReview-resultUser-cache",
				reviewUserCacheKey:"mkt-dd-salesOrderReview-reviewUser-cache",
				productListLength:0
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','orgId'])
		},
		methods:{
			getWorkParam(){
				return{
					productListLength:this.productListLength
				}
			},
			initSubList(list,listName,title){
				let _that = this
				let subList = _that._.cloneDeep(list)
				_that.subEditorObj[title].workList = subList
				_that.$refs.autoform.$set(_that.$refs.autoform.formdata,listName,JSON.stringify(subList))
				_that.$set(_that.formdata,listName,JSON.stringify(subList))
			},
			formSubmit(){
				let _that = this
				_that.formdata.orgId = _that.orgId
				if(_that.edit){
					_that.om.formSave("salesOrderReview/doUpdate",_that.formdata,_that)
				}else if(_that.checkedit){
					_that.om.formSave("commonForm/doCheckOrApproveUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("salesOrderReview/doAdd",_that.formdata,_that)
				}
			},
			getReviewProductList(id){
				let _that = this
				_that.om.request(
					"salesOrderReview/findReviewProductList",
					{
						formId:"", 
						customerRequirementId:id
					},
					"post"
				).then(res=>{
					for(let item of res.value){
						if(!item.fileList){
							item.fileList = []
						}
					}
					_that.subEditorObj["title3"].workList = res.value
					_that.$refs.autoform.changeConfigAttr("title3", "length", res.value.length || 0)
					_that.productListLength = res.value.length
					errorModal(_that, '提示信息', " 加载了" + (res.value.length || 0 )+ "条产品信息")
				})
			},
			getListByOrg(id){
				let _that  = this
				_that.om.request(
					"orgInstPosUserOfficial/findListByOrg",
					{
						posCanBeNull: false,
						oldValue: id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.userPostId = res[0].id
					_that.$refs.autoform.formdata.userName = res[0].userPosName
					_that.$refs.autoform.formdata.postId = res[0].posId
					_that.$refs.autoform.formdata.postName = res[0].posName
					_that.$refs.autoform.formdata.instId = res[0].instId
					_that.$refs.autoform.formdata.instName = res[0].instName
					_that.$refs.autoform.formdata.mobile = res[0].autoMobile
				})
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.sysdictComboboxUrl("38555"),
					urlUtil.sysdictComboboxUrl("38525"),
					urlUtil.sysdictComboboxUrl("38526"),
					urlUtil.sysdictComboboxUrl("38527"),
					
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"salesOrderReview/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("title1","customerRequirementId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({}),
							url:"customerRequirement/findReviewPager",
							initload:true,
							cachename:_that.requirementCache,
							searchfield:"q",
							namefield:"code",
							notefield:"customerName",
							extrafield:"customerCode",
						}
					},null,mainData)
					changeEditor("title1","typeId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor("title2_1","orderTypeId","data",configSelectData(res[2],"id","dicname"),mainData)
					changeEditor("title2_1","saleTypeId","data",configSelectData(res[3],"id","dicname"),mainData)
					changeEditor("title2_1","trafficTypeId","data",configSelectData(res[4],"id","dicname"),mainData)
					changeEditor('title2','userPostId',{
							link: '../../public/pageSearch/pageSearch',
							"linkParam": {
								other: JSON.stringify({
								}),
								namefield: urlUtil.officePageUrl().namefield,
								searchfield: 'q',
								url: urlUtil.officePageUrl().url,
								cachename: _that.userCacheKey,
								placeholder: urlUtil.officePageUrl().prompt
							}
						},null,mainData);
						changeEditor('title6','resultUserPostId',{
								link: '../../public/pageSearch/pageSearch',
								"linkParam": {
									other: JSON.stringify({
									}),
									namefield: urlUtil.officePageUrl().namefield,
									searchfield: 'q',
									url: urlUtil.officePageUrl().url,
									cachename: _that.resultUserCacheKey,
									placeholder: urlUtil.officePageUrl().prompt
								}
							},null,mainData);
							changeEditor('title6','reviewUserPostId',{
									link: '../../public/pageSearch/pageSearch',
									"linkParam": {
										other: JSON.stringify({
										}),
										namefield: urlUtil.officePageUrl().namefield,
										searchfield: 'q',
										url: urlUtil.officePageUrl().url,
										cachename: _that.reviewUserCacheKey,
										placeholder: urlUtil.officePageUrl().prompt
									}
								},null,mainData);
					if(_that.subEditorObj["title3"]){
						changeConfigAttr("title3", "showLength", true, mainData)
						changeEditor("title3","productStr",{
							showLength:true,
							length:0,
							getWorkParam:'getWorkParam',
							titleTemplate:"产品编码:<%= projectCode || '未填写' %>",
							subtitle1Template:"产品名称:<%= projectName %>",
							subtitle2Template:"规格型号:<%=projectStd%>|单位:<%=projectUnit%>|数量:<%=projectNum%>"
						},null,mainData)
					}
					if(_that.subEditorObj["title7"]){
						changeEditor("title7","userStr",{
							titleTemplate:"评审内容:<%= reviewContent || '未填写' %>",
							subtitle1Template:"评审人:<%= reviewUserName %>",
						},null,mainData)
					}
					if(_that.subEditorObj["title7_1"]){
						changeEditor("title7_1","userOtherStr",{
							titleTemplate:"评审内容:<%= reviewContent || '未填写' %>",
							subtitle1Template:"评审人:<%= reviewUserName || '未填写' %>",
							},null,mainData)
					}
					if(_that.subEditorObj["title8"]){
						let att = {}
						let configData = _that.subConfigData['title8']
						if(hasEditor('title8','materielId',configData)){
							if(_that.checkedit){
								att.showDel = false
							}
							let title = getEditorTitle("title8","materielId",configData)
							att.titleTemplate = title+":<%= materielCode || '未填写' %>"
							let title1 = getEditorTitle("title8","materielName",configData)
							att.subtitle1Template = title1+":<%= materielName || '未填写' %>"
						}
						changeEditor("title8","materielStr",att,mainData)
					}
					if(_that.subEditorObj["title9"]){
						changeEditor("title9","workDataStr",{
							titleTemplate:"代码:<%= relateCode3 || '未填写' %>",
							subtitle1Template:"工作项名称:<%= workName3 %>",
						},null,mainData)
					}
					
					if(_that.subEditorObj["title9_1"]){
						changeEditor("title9_1","riskStr",{
							titleTemplate:"评审内容:<%= reviewRiskName || '未填写' %>",
						},null,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id",'userId',"resultUserId","reviewLeaderId")
						if(_that.checkedit){
							fieldArr.push("resultUserId")
						}
						for(let index in res[5].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[5].value[index])
								_that.$set(_that.formdata,index,res[5].value[index])
							}
						}
						_that.initSubList(res[5].value.productList,"productStr","title3")
						changeConfigAttr("title3", "length", res[5].value.productList.length || 0,mainData)
						delete res[5].value.productList
						_that.initSubList(res[5].value.userList,"userStr","title7")
						delete res[5].value.userList
						_that.initSubList(res[5].value.otherUserList,"userOtherStr","title7_1")
						delete res[5].value.otherUserList
						_that.initSubList(res[5].value.materielList,"materielStr","title8")
						delete res[5].value.materielList 
						_that.initSubList(res[5].value.workDataList,"workDataStr","title9")
						delete res[5].value.workDataList
						_that.initSubList(res[5].value.riskList,"workDataStr","title9_1")
						delete res[5].value.riskList
						if(_that.checkedit && _that.userId == _that.$refs.autoform.formdata.resultUserId){
							changeEditor("title9","workDataStr",{
								showDel:true
							},null,mainData)
						}else{
							changeEditor("title9","workDataStr",{
								showDel:false
							},null,mainData)
							changeEditor("title10","reviewLeaderRemark",'type','',mainData)
							changeEditor("tilte11","expectDeliveryDate",'type','',mainData)
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("title1","typeId")
							_that.$refs.autoform.initSelectIndex("title2_1","orderTypeId")
							_that.$refs.autoform.initSelectIndex("title2_1","saleTypeId")
							_that.$refs.autoform.initSelectIndex("title2_1","trafficTypeId")
							})
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							if(_that.checkedit){
								for(let item of _that.subEditorObj["title8"].workList){
									if(!item.planUserId || item.planUserId == _that.userId){
										item.showDel = true
									}
								}
							}
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.requirementCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerRequirementCode = d.code
							formdata.formZuserId = d.zuserId
							formdata.formZuserName = d.zuserName
							formdata.formZtime = d.ztime
							formdata.customerCode = d.customerCode
							formdata.customerShortName = d.customerShortName
							formdata.erpCode = d.erpCode
							formdata.customerName = d.customerName
							formdata.receiveUser = d.receiveUser
							formdata.receiveUserMobile = d.receiveUserMobile
							formdata.receiveAddress = d.receiveAddress
							formdata.trafficTypeId = d.trafficTypeId
							formdata.trafficTypeName = d.trafficTypeName
							_that.getListByOrg(d.userPostId)
							_that.getReviewProductList(d.id)
							_that.$refs.autoform.initSelectIndex("title2_1","trafficTypeId")
							uni.removeStorage({
								key:_that.requirementCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userPostId = d.id
							formdata.userName = d.userPosName
							formdata.userId = d.userId
							formdata.postName = d.posName
							formdata.postId = d.posId
							formdata.instId = d.instId
							formdata.instName = d.instName
							formdata.mobile = d.mobile
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.resultUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.resultUserPostId = d.id
							formdata.resultUserName = d.userPosName
							formdata.resultUserId = d.userId
							formdata.resultPostId =  d.posId
							formdata.resultPostName = d.posName
							formdata.resultInstId = d.instId
							formdata.resultInstName = d.instName
							uni.removeStorage({
								key:_that.resultUserCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.reviewUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.reviewUserPostId = d.id
							formdata.reviewLeaderName = d.userPosName
							formdata.reviewLeaderId = d.userId
							formdata.reviewLeaderPostId =  d.posId
							formdata.reviewLeaderPostName = d.posName
							formdata.reviewLeaderInstId = d.instId
							formdata.reviewLeaderInstName = d.instName
							uni.removeStorage({
								key:_that.reviewUserCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>
